<template>
  <div class="prcList">
    <div class="tabBox">
      <div class="clickBox">
        <div class="block">
          <span class="demonstration">开始时间：</span>
          <input type="text" v-model="time1">
          <span class="demonstration">结束时间：</span>
          <input type="text" v-model="time2">
          <bottom name="bottom" @click="clickEnt(1)">查询</bottom>

          <span>订单数：{{number}}</span>
          <span>营业额：{{allA}}</span>
          <span>公司余额：{{allB}}</span>
          <span>总纯利润：{{allPrice}}</span>
        </div>
      </div>
    </div>
    <Loading v-if="loading"></Loading>
    <div class="info orderBox orderInfo" v-else>
      <table class="listInfo mt20" v-for="(item,index) in tableData">
        <colgroup>
            <col width="6%">
            <col width="4%">
            <col width="18%">
            <col width="4%">
            <col width="4%">
            <col width="4%">
            <col width="4%">
            <col width="4%">
            <col width="4%">
            <col width="4%">
            <col width="4%">
            <col width="4%">
            <col width="4%">
            <col width="4%">
        </colgroup>
        <thead>
          <tr>
            <td>时间</td>
            <td>订单编号</td>
            <td>产品</td>
            <td>产品编号</td>
            <td>数量</td>
            <td>成本</td>
            <td>商品成本</td>
            <td>零售价</td>
            <td>总售价</td>
            <td>优惠</td>
            <td>折后价</td>
            <td>收入</td>
            <td>纯利润</td>
            <td>订单状态</td>
            <td>订单信息</td>
            <!-- <td>操作</td> -->
          </tr>
        </thead>
        <tbody>
          <tr class="orderList" >
            <td>{{item.created_at}}</td>
            <td>{{item.trade_no}}</td>
            <td>
              <p v-for="list in item.order_items">{{list.goods_name}}</p>
            </td>
            <td>
              <p style="text-align: center;" v-for="list in item.order_items">{{list.goods_sn}}</p>
            </td>
            <td>
              <p style="text-align: center;" v-for="list in item.order_items">{{list.goods_count}}</p>
            </td>
            <td>
              <p style="text-align: center;" v-for="list in item.order_items">{{list.market_price}}</p>
            </td>
            <td>
              {{item.order_items|allget}}
            </td>
            <td>
              <p style="text-align: center;" v-for="list in item.order_items">{{list.goods_price}}</p>
            </td>
            <td>
              {{item.order_items|allpost}}
            </td>
            <td>{{item.ratio}}</td>
            <td>{{item|aa}}</td>
            <td>
              {{item|bb}}
            </td>
            <td>{{item|cc}}</td>
            <td>{{item.status|state}}</td>
            <td v-if="item.user_address">
              {{item.user_address.post_code}},
              {{item.user_address.address}},
              {{item.user_address.first_name}},
              {{item.user_address.mobile}}
            </td>
            <!-- <td><router-link tag="span" class="print" :to="{path:'/orderPrint',query:{id:item.id}}">打印</router-link></td> -->
          </tr>
        </tbody>
      </table>
  </div>
  </div>
</template>
<script>
import Vue from 'vue'
import Loading from '@/components/loading'
import axios from 'axios'
import { Get, Post ,Put } from '@/config/api'
export default {
  name: 'orderShow',
  data() {
    return {
      loading: true,
      tableData: [],
      tableNum: 300, //每页显示多少条
      tableCont: 0, //一共有多少条信息
      tablePage: 1, //当前页码
      time1:'',//选择开始时间
      time2:'',//选择结束时间
      number:'',//订单数
      numData:[]
    }
  },
  filters:{
      state(vule){
        if(vule=='1'){
          return '未处理'
        }else if(vule=='2'){
          return '送货中'
        }else if(vule=='3'){
          return '已完成'
        }else if(vule=='4'){
          return '已取消'
        }
      },
      type(vule){
        if(vule=='1'){
          return '中通'
        }else if(vule=='2'){
          return '圆通'
        }else if(vule=='3'){
          return '顺风'
        }
      },
      allget(val){
        let len=val.length
        let all=0;
        for(let i=0;i<len;i++){
          all+=val[i].goods_count*val[i].market_price
        }
        return all.toFixed(2); 
      },
      allpost(val){
        let len=val.length
        let all=0;
        for(let i=0;i<len;i++){
          all+=val[i].goods_count*val[i].goods_price
        }
        return all.toFixed(2); 
      },
      //优惠后多少钱
      aa(val){
        let len=val.order_items.length
        let all=0;
        for(let i=0;i<len;i++){
          all+=val.order_items[i].goods_count*val.order_items[i].goods_price*val.ratio/100
        }
        return all.toFixed(2); 
      },
      //单利润
      /*bb(val){
        let ka=0;
        ka=parseFloat(val.goods_price)-parseFloat(val.market_price)
        let num = ka.toFixed(2); 
        return num
      },*/
      bb(val){
        let len=val.order_items.length
        let all=0;
        let ka=0;
        let kb=0;
        let date=val.order_items
        for(let i=0;i<len;i++){
          ka+=parseFloat(date[i].goods_price*date[i].goods_count)
        }
        for(let i=0;i<len;i++){
          kb+=parseFloat(date[i].market_price*date[i].goods_count)
        }
        all=(ka*val.ratio/100)-kb
        let num = all.toFixed(2); 
        return num
      },
      //总利润
      cc(val){
        let len=val.order_items.length
        let all=0;
        let ka=0;
        let kb=0;
        let date=val.order_items
        for(let i=0;i<len;i++){
          ka+=parseFloat(date[i].goods_price*date[i].goods_count)
        }
        for(let i=0;i<len;i++){
          kb+=parseFloat(date[i].market_price*date[i].goods_count)
        }
        if(val.user.is_vip==2){
          all=(ka*val.ratio/100)-kb
        }else{
          all=(ka*val.ratio/100)-kb-2
        }
        let num = all.toFixed(2); 
        return num
      }
      //所以订单利润
  },
  computed:{
    //总利润
    allPrice(){
      let len=this.numData.length
      let date=this.numData
      let kb=0
      for(let i=0;i<len;i++){
        let kdate=date[i].order_items
        let klen=date[i].order_items.length
        let jiage=date[i].order_amount
        let ka=0
        for(let j=0;j<klen;j++){
          ka+=kdate[j].market_price*kdate[j].goods_count
        }
        let kk=0
        if(date[i].user.is_vip==2){
          kk=jiage-ka
        }else{
          kk=jiage-ka-2
        }
        kb+=kk
      }
      return kb.toFixed(2)
    },
    //营业额
    allA(){
      let len=this.numData.length
      let date=this.numData
      let kb=0
      for(let i=0;i<len;i++){
        let jiage=date[i].order_amount
        kb+=jiage
      }
      return kb.toFixed(2)
    },
    //总收入
    allB(){
      let len=this.numData.length
      let date=this.numData
      let kb=0
      for(let i=0;i<len;i++){
        if(date[i].user.is_vip==2){
          let jiage=date[i].order_amount
          kb+=jiage
        }else{
          let jiage=date[i].order_amount-2
          kb+=jiage
        }
      }
      return kb.toFixed(2)
    }
  },
  mounted() {
    this.getData(1)
    
  },
  components: {
    Loading
  },
  methods: {
    //获取数据
    async getData(val) {
      // let myDate = new Date();
      // console.log(myDate.getTime())
      let reg=await Get('/order/all?_include=user_address,order_items,user,pay_type&_per_page='+this.tableNum+'&page='+val,this.$store.state.token)
      reg.data.forEach((val)=>{
        if(val.status==3){
          this.numData.push(val)
        }
      })
      this.number=reg.meta.pagination.count
      this.tableData=reg.data;
      this.tableCont=reg.meta.pagination.total
      this.loading=false
    },
    //分页
    handleCurrentChange(val) {
      this.tablePage = val
      this.clickEnt(val)
    },
    async changeStart(e){
        if (this.Check()) {
          let data={
            "status":e.status
          }
          let reg=await Put('/order/status/'+e.id,this.$store.state.token,data)
        }
    },
    //点击获取查询
    async clickEnt(val){
      this.loading=true
      let reg=await Get('/order/all?_include=user_address,order_items,user,pay_type&time_begin='+this.time1+'&time_end='+this.time2+'&_per_page='+this.tableNum+'&page='+val,this.$store.state.token)
      this.numData=[]
      reg.data.forEach((val)=>{
        if(val.status==3){
          this.numData.push(val)
        }
      })
      this.number=reg.meta.pagination.count
      this.tableData=reg.data;
      this.tableCont=reg.meta.pagination.total
      this.loading=false
    }
  }
}


</script>

